package com.avit.tv.kui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.avit.tv.kui.background.BgDrawable;
import com.avit.tv.kui.base.BaseFocusLayout;
import com.avit.tv.kui.dialog.DialogClickListener;

/**
 * 弹窗
 *
 * @author like
 * @date 2020/6/23
 */
public class AvitDialog extends BaseFocusLayout implements View.OnClickListener, View.OnFocusChangeListener {

    private String title;
    private String content;
    private String okText;
    private String cancelText;
    private int titleColor;
    private int contentColor;
    private int okTextColor;
    private int cancelTextColor;
    private int okButtonColor;
    private int cancelButtonColor;
    private int titleSize;
    private int contentSize;
    private int buttonSize;
    private TextView titleView;
    private TextView contentView;
    private TextView okView;
    private TextView cancelView;
    private BgDrawable buttonFocusDrawable;
    private BgDrawable okButtonDrawable;
    private BgDrawable cancelButtonDrawable;
    private int buttonFocusColor;
    private int buttonFocusTextColor;
    private boolean isOnlyOneButton;

    private DialogClickListener dialogClickListener;

    public AvitDialog(@NonNull Context context) {
        super(context);
    }

    public AvitDialog(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public AvitDialog(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.AvitDialog, defStyleAttr, 0);
        title = typedArray.getString(R.styleable.AvitDialog_dialog_title);
        content = typedArray.getString(R.styleable.AvitDialog_dialog_content);
        okText = typedArray.getString(R.styleable.AvitDialog_ok_text);
        cancelText = typedArray.getString(R.styleable.AvitDialog_cancel_text);
        titleColor = typedArray.getColor(R.styleable.AvitDialog_dialog_title_color, Color.WHITE);
        contentColor = typedArray.getColor(R.styleable.AvitDialog_dialog_content_color, Color.WHITE);
        okTextColor = typedArray.getColor(R.styleable.AvitDialog_ok_text_color, Color.WHITE);
        cancelTextColor = typedArray.getColor(R.styleable.AvitDialog_cancel_text_color, Color.WHITE);
        okButtonColor = typedArray.getColor(R.styleable.AvitDialog_ok_button_color, Color.TRANSPARENT);
        cancelButtonColor = typedArray.getColor(R.styleable.AvitDialog_cancel_button_color, Color.TRANSPARENT);
        buttonFocusColor = typedArray.getColor(R.styleable.AvitDialog_button_focus_color, Color.TRANSPARENT);
        buttonFocusTextColor = typedArray.getColor(R.styleable.AvitDialog_button_focus_text_color, Color.WHITE);
        titleSize = typedArray.getDimensionPixelSize(R.styleable.AvitDialog_dialog_title_size, DEFAULT_TITLE_SIZE);
        contentSize = typedArray.getDimensionPixelSize(R.styleable.AvitDialog_dialog_content_size, DEFAULT_SIZE);
        buttonSize = typedArray.getDimensionPixelSize(R.styleable.AvitDialog_button_size, DEFAULT_BUTTON_SIZE);
        isOnlyOneButton = typedArray.getBoolean(R.styleable.AvitDialog_is_only_one_button, false);
        typedArray.recycle();
    }

    @Override
    protected void initContent(Context context) {
        titleView = new AvitPartChildMarqueeView(context);
        contentView = new TextView(context);
        okView = new TextView(context);
        cancelView = new TextView(context);
        initContent();
    }

    private void initContent() {
        initTextView(titleView, titleSize, titleColor, title);
        initTextView(contentView, contentSize, contentColor, content);
        int buttonTextSize = buttonSize / 2;
        initTextView(okView, buttonTextSize, okTextColor, okText);
        initTextView(cancelView, buttonTextSize, cancelTextColor, cancelText);
        setMinimumHeight(2*buttonSize*9/3);
        setMinimumWidth(2*buttonSize*16/3);
    }

    private void initTextView(TextView textView, int size, int color, String value) {
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
        textView.setTextColor(color);
        textView.setText(value);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
        textView.setLayoutParams(layoutParams);
        initContentLayout();
        initButtonShow();
    }

    private void initContentLayout() {
        contentView.setPadding(0, titleSize + DEFAULT_SIZE, 0, 0);
        contentView.setFocusable(false);
        contentView.setFocusableInTouchMode(false);
        titleView.setFocusable(false);
        titleView.setFocusable(false);
        setPadding(DEFAULT_SIZE, DEFAULT_SIZE, DEFAULT_SIZE, DEFAULT_SIZE);
    }

    private void initButtonView(TextView textView) {
        int paddingVerticalSize = buttonSize / 3;
        int paddingHorizontalSize = paddingVerticalSize * 3;
        textView.setPadding(paddingHorizontalSize, paddingVerticalSize, paddingHorizontalSize, paddingVerticalSize);
        textView.setOnClickListener(this);
        textView.setFocusable(true);
        textView.setFocusableInTouchMode(true);
        textView.setOnFocusChangeListener(this);
        if (textView.hasFocus()){
            initShow(textView, buttonFocusDrawable, buttonFocusColor,buttonFocusTextColor);
        }
    }

    private void initOkView() {
        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.BOTTOM | Gravity.START;
        layoutParams.leftMargin = buttonSize;
        okView.setLayoutParams(layoutParams);
        initShow(okView, okButtonDrawable, okButtonColor,okTextColor);
        initButtonView(okView);
    }

    private void initCancelView() {
        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.BOTTOM | Gravity.END;
        layoutParams.rightMargin = buttonSize;
        cancelView.setLayoutParams(layoutParams);
        initShow(cancelView, cancelButtonDrawable, cancelButtonColor,cancelTextColor);
        initButtonView(cancelView);
    }

    /**
     * 判断是否关闭或显示button
     */
    private void initButtonShow(){
        if (isOnlyOneButton){
            LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
            cancelView.setVisibility(GONE);
            okView.setLayoutParams(layoutParams);
        }else {
            cancelView.setVisibility(VISIBLE);
            initOkView();
            initCancelView();
        }
    }

    @Override
    protected void insertView() {
        addView(titleView);
        addView(contentView);
        addView(okView);
        addView(cancelView);
    }

    /**
     * 设置dialog 标题
     *
     * @param title
     */
    public void setTitle(String title) {
        this.title = title;
        titleView.setText(title);
    }

    /**
     * 设置dialog 内容
     *
     * @param content
     */
    public void setContent(String content) {
        this.content = content;
        contentView.setText(content);
    }

    /**
     * 设置dialog 确定文字
     *
     * @param okText
     */
    public void setOkText(String okText) {
        this.okText = okText;
        okView.setText(okText);
    }

    /**
     * 设置dialog 取消文字
     *
     * @param cancelText
     */
    public void setCancelText(String cancelText) {
        this.cancelText = cancelText;
        cancelView.setText(cancelText);
    }

    /**
     * 设置标题文字颜色
     *
     * @param titleColor
     */
    public void setTitleColor(@ColorInt int titleColor) {
        this.titleColor = titleColor;
        titleView.setTextColor(titleColor);
    }

    /**
     * 设置内容文字颜色
     *
     * @param contentColor
     */
    public void setContentColor(@ColorInt int contentColor) {
        this.contentColor = contentColor;
        contentView.setTextColor(contentColor);
    }

    /**
     * 设置确定文字颜色
     *
     * @param okTextColor
     */
    public void setOkTextColor(@ColorInt int okTextColor) {
        this.okTextColor = okTextColor;
        okView.setTextColor(okTextColor);
    }

    /**
     * 设置取消文字颜色
     *
     * @param cancelTextColor
     */
    public void setCancelTextColor(@ColorInt int cancelTextColor) {
        this.cancelTextColor = cancelTextColor;
        cancelView.setTextColor(cancelTextColor);
    }

    /**
     * 设置确定按钮背景颜色
     *
     * @param okButtonColor
     */
    public void setOkButtonColor(@ColorInt int okButtonColor) {
        this.okButtonColor = okButtonColor;
        okView.setBackgroundColor(okButtonColor);
    }

    /**
     * 设置确定自定义的drawable背景
     *
     * @param drawable
     */
    public void setOkButtonDrawable(BgDrawable drawable) {
        this.okButtonDrawable = drawable;
        if (drawable!=null)
        drawable.targetDrawable(okView);
    }

    /**
     * 设置取消按钮背景颜色
     *
     * @param cancelButtonColor
     */
    public void setCancelButtonColor(@ColorInt int cancelButtonColor) {
        this.cancelButtonColor = cancelButtonColor;
        cancelView.setBackgroundColor(cancelButtonColor);
    }

    /**
     * 设置取消自定义的drawable背景
     *
     * @param drawable
     */
    public void setCancelButtonDrawable(BgDrawable drawable) {
        this.cancelButtonDrawable = drawable;
        if (drawable!=null)
        drawable.targetDrawable(cancelView);
    }

    /**
     * 设置标题文字尺寸
     *
     * @param titleSize
     */
    public void setTitleSize(int titleSize) {
        this.titleSize = titleSize;
        setTextSize(titleView, titleSize);
        initContent();
    }

    /**
     * 设置内容文字尺寸
     *
     * @param contentSize
     */
    public void setContentSize(int contentSize) {
        this.contentSize = contentSize;
        setTextSize(contentView, contentSize);
        initContent();
    }

    /**
     * 设置相对button的大小
     *
     * @param buttonSize
     */
    public void setButtonSize(int buttonSize) {
        this.buttonSize = buttonSize;
        initContent();
    }

    /**
     * 设定弹窗按钮focus的时候显示的drawable
     * @param buttonFocusDrawable
     */
    public void setButtonFocusDrawable(BgDrawable buttonFocusDrawable) {
        this.buttonFocusDrawable = buttonFocusDrawable;
        initContent();
    }

    /**
     * 设定弹窗按钮focus的时候所显示的颜色
     * @param buttonFocusColor
     */
    public void setButtonFocusColor(int buttonFocusColor) {
        this.buttonFocusColor = buttonFocusColor;
        initContent();
    }

    /**
     * 设置获取焦点过后的dialog的按键显示颜色
     * @param buttonFocusTextColor
     */
    public void setButtonFocusTextColor(int buttonFocusTextColor) {
        this.buttonFocusTextColor = buttonFocusTextColor;
    }

    /**
     * dialog click 的回调
     *
     * @param dialogClickListener
     */
    public void setDialogClickListener(DialogClickListener dialogClickListener) {
        this.dialogClickListener = dialogClickListener;
    }

    /**
     * 刷新焦点锁于哪个button
     * @param isOkFocus
     */
    public void initFocus(boolean isOkFocus) {
        if (isOkFocus) okView.requestFocus();
        else cancelView.requestFocus();
    }

    /**
     * 设置是否只显示一个button
     * @param onlyOneButton
     * @return
     */
    public void setOnlyOneButton(boolean onlyOneButton) {
        isOnlyOneButton = onlyOneButton;
        initButtonShow();
    }

    @Override
    public void onClick(View v) {
        if (dialogClickListener == null) return;
        String hashCode = getHashViewCode(v);
        if (hashCode.equals(getHashViewCode(okView))) {
            dialogClickListener.onConfirm();
        } else if (hashCode.equals(getHashViewCode(cancelView))) {
            dialogClickListener.onCancel();
        }
    }


    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            initShow(v, buttonFocusDrawable, buttonFocusColor,buttonFocusTextColor);
        } else {
            if (getHashViewCode(v).equals(getHashViewCode(okView))) {
                initShow(okView, okButtonDrawable, okButtonColor,okTextColor);
            } else if (getHashViewCode(v).equals(getHashViewCode(cancelView))) {
                initShow(cancelView, cancelButtonDrawable, cancelButtonColor,cancelTextColor);
            }
        }
    }

    private void initShow(View view, BgDrawable drawable, @ColorInt int color, @ColorInt int textColor) {
        if (drawable != null) {
            drawable.targetDrawable(view);
        } else {
            view.setBackgroundColor(color);
        }
        if (view instanceof TextView)
            ((TextView) view).setTextColor(textColor);
    }

}
